package com.se.frontline.layers.menu;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.andengine.entity.modifier.FadeInModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;

import com.se.frontline.MainActivity;
import com.se.frontline.gameplay.heroes.Hero;
import com.se.frontline.gameplay.units.Unit;
import com.se.frontline.gameplay.units.Unit1;
import com.se.frontline.layers.FrontlineHUD;
import com.se.frontline.layers.ManagedMenuLayer;
import com.se.frontline.layers.HUDs.Joystick;
import com.se.frontline.managers.ResourceManager;

public class FinishedMenuLayer extends ManagedMenuLayer {

	//====================================================
	// CONSTANTS
	//====================================================
	private static FinishedMenuLayer INSTANCE = new FinishedMenuLayer();
	
	//====================================================
	// VARIABLES
	//====================================================
	// the Main Menu, managed as a HUD
	public static HUD FinishedMenuHUD = new HUD();
	
	//Sprites
	private ButtonSprite playButton;	
	private ButtonSprite finishedSprite;
	private ButtonSprite restartButton;
	private ButtonSprite multiplayerButton;
	private ButtonSprite endButton;
	private ButtonSprite starSprite;
	private ButtonSprite menuSprite;
	
	//Display Sections
	private static final float RIGHT_MARGIN = 1f * ResourceManager
			.getInstance().cameraWidth - 10;
	private static final float MENU_BUTTONS_LEFT = 0.1f * ResourceManager
			.getInstance().cameraWidth +50;
	
	//====================================================
	// CONSTRUCTOR
	//====================================================
	public FinishedMenuLayer() {
	}
	
	private void setMenuHUD() {
		/*Rectangle ground = new Rectangle(ResourceManager.getInstance().cameraWidth * 0.1f, 0,
				ResourceManager.getInstance().cameraWidth * 0.8f,
				(float) (ResourceManager.getInstance().cameraHeight * 1),
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager());
		ground.setColor(0.9f, 0.6f, 0.3f);
		MainMenuHUD.attachChild(ground);*/
		menuSprite = new ButtonSprite(0, 0, ResourceManager.getInstance().menuTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {};

		FinishedMenuHUD.attachChild(menuSprite);
	}
	
	//Create the playButton
	private void loadplayButton() {

		playButton = new ButtonSprite(RIGHT_MARGIN - 50, 10, ResourceManager.getInstance().playHUDTextureRegion,
				ResourceManager.getInstance().getEngine()
						.getVertexBufferObjectManager()) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					
						//this.dettachFrontlineHUD();
					System.out.println("sdfposdfjsodfsdopfjs");
					FinishedMenuLayer.getInstance().FadeOut();
					
					
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		FinishedMenuHUD.registerTouchArea(playButton);
		FinishedMenuHUD.attachChild(playButton);
	}
	
	//Create the optionsButton
			private void loadfinishedSprite() {

				finishedSprite = new ButtonSprite(MENU_BUTTONS_LEFT, 10, ResourceManager.getInstance().finishedTextureRegion,
						ResourceManager.getInstance().getEngine()
								.getVertexBufferObjectManager()) {
				};
				
				FinishedMenuHUD.attachChild(finishedSprite);
			}
			
			//Create the optionsButton
			private void loadmultiplayerButton() {

				multiplayerButton = new ButtonSprite(MENU_BUTTONS_LEFT + 300, 225, ResourceManager.getInstance().multiplayerHUDTextureRegion,
						ResourceManager.getInstance().getEngine()
								.getVertexBufferObjectManager()) {
					@Override
					public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
							final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
						if (pSceneTouchEvent.isActionDown()) {
							
								//this.dettachFrontlineHUD();
							FinishedMenuLayer.getInstance().FadeOut();
							
							
						}
						return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
					}
					};
				FinishedMenuHUD.registerTouchArea(multiplayerButton);
				FinishedMenuHUD.attachChild(multiplayerButton);
			}
			
			//Create the restartButton
			private void loadrestartButton() {

				restartButton = new ButtonSprite(MENU_BUTTONS_LEFT, 225, ResourceManager.getInstance().restartHUDTextureRegion,
						ResourceManager.getInstance().getEngine()
								.getVertexBufferObjectManager()) {
					@Override
					public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
							final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
						if (pSceneTouchEvent.isActionDown()) {
							
								//this.dettachFrontlineHUD();
							FinishedMenuLayer.getInstance().FadeOut();
							
							
						}
						return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
					}
				};
				FinishedMenuHUD.registerTouchArea(restartButton);
				FinishedMenuHUD.attachChild(restartButton);
			}
			
			//Create the endButton
			private void loadendButton() {

				endButton = new ButtonSprite(MENU_BUTTONS_LEFT+150, 225, ResourceManager.getInstance().endHUDTextureRegion,
						ResourceManager.getInstance().getEngine()
								.getVertexBufferObjectManager()) {
					@Override
					public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
							final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
						if (pSceneTouchEvent.isActionDown()) {
							
								//this.dettachFrontlineHUD();
							FinishedMenuLayer.getInstance().FadeOut();
							
							
						}
						return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
					}
				};
				FinishedMenuHUD.registerTouchArea(endButton);
				FinishedMenuHUD.attachChild(endButton);
			}
			
			//Create Star
			private void loadstarSprite(int Anzahl) {
				if(Anzahl>2){
				starSprite = new ButtonSprite(MENU_BUTTONS_LEFT+400, 150, ResourceManager.getInstance().starTextureRegion,
						ResourceManager.getInstance().getEngine()
								.getVertexBufferObjectManager()) {};
				FinishedMenuHUD.attachChild(starSprite);}
				if(Anzahl>1){
					starSprite = new ButtonSprite(MENU_BUTTONS_LEFT+250, 150, ResourceManager.getInstance().starTextureRegion,
							ResourceManager.getInstance().getEngine()
									.getVertexBufferObjectManager()) {};
					FinishedMenuHUD.attachChild(starSprite);}
				if(Anzahl>0){
					starSprite = new ButtonSprite(MENU_BUTTONS_LEFT+100, 150, ResourceManager.getInstance().starTextureRegion,
							ResourceManager.getInstance().getEngine()
									.getVertexBufferObjectManager()) {};
					FinishedMenuHUD.attachChild(starSprite);}
			}
			
	
		
	
	//====================================================
	// GETTERS AND SETTERS
	//====================================================
	public static FinishedMenuLayer getInstance() {
		if(INSTANCE == null)
			INSTANCE = new FinishedMenuLayer();
		return INSTANCE;
	}
	
	
	//====================================================
	// METHODES
	//====================================================
	private Text text;
	public int time = 0;
	public String Zeit;
	
	public void textanzeige(){
		
		
	                Zeit = FrontlineHUD.getTime(Zeit);
	text = new Text(0, 0, MainActivity.font, Zeit+" Sekunden benoetigt", ResourceManager.getInstance().getEngine()
			.getVertexBufferObjectManager());
	 
	                
	  text.setPosition(200, 400);
	  

	 FinishedMenuHUD.attachChild(text);
	        
	

	}
	
	public void FadeIn() {
		if(FinishedMenuHUD.getChildCount() == 0)
		{	FadeInModifier fadi = new FadeInModifier(0.5f);
			this.setMenuHUD();
			this.loadplayButton();
			this.loadfinishedSprite();
			this.loadrestartButton();
			this.loadendButton();
			this.textanzeige();
			this.loadstarSprite(3);
			this.loadmultiplayerButton();
			
			
		}
		ResourceManager.getInstance().getCamera().setHUD(FinishedMenuHUD);
	}
	
	public void FadeOut() {
		ResourceManager.getInstance().getCamera().setHUD(FrontlineHUD.HUDRegion);
	}
}